package com.vote.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.vote.dao.util.HibernateDao;
import com.vote.entity.PrizeInfo;
import com.vote.util.VoteConstants;

@Repository
public class PrizeInfoDao extends HibernateDao<PrizeInfo, Long> {
	
	private static Logger logger = LoggerFactory.getLogger(PrizeInfoDao.class);
	
	public PrizeInfo bindIpPrize(String ip){
		List<PrizeInfo> plst=this.createQuery("from PrizeInfo p where p.bindIp=? and p.status="+VoteConstants.PRIZE_STATUS_NOT_REDEEM, ip).list();
		if(plst==null || plst.isEmpty()){
			return null;
		}else{
			return plst.get(0);
		}
	}	
	//根据奖品级别取奖品
	public PrizeInfo getPrizeInfoByLevel(Long prizeLevel){
		List<PrizeInfo> plst=this.createQuery("from PrizeInfo p where p.prizeSetting.prizeLevel=? and p.status=? and p.bindIp is null order by id",prizeLevel,VoteConstants.PRIZE_STATUS_NOT_REDEEM).list();
		if(plst==null || plst.isEmpty()){
			return null;
		}else{
			return plst.get(0);
		}
	}	
	//
	public PrizeInfo getPrizeInfoByIPCookie(String ip,String cookie){
		List<PrizeInfo> plst=this.createQuery("from PrizeInfo p where p.ip=? and p.cookie=? order by id desc",ip,cookie).list();
		if(plst==null || plst.isEmpty()){
			return null;
		}else{
			return plst.get(0);
		}
	}
}